package org.codehaus.mojo.wagon;

import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.wagon.ConnectionException;
import org.apache.maven.wagon.Wagon;
import org.apache.maven.wagon.WagonException;

import java.io.IOException;

public abstract class AbstractCopyMojo extends AbstractDoubleWagonMojo {

    protected abstract void copy(Wagon src, Wagon target) throws IOException, WagonException;

    @Override
    public void execute() throws MojoExecutionException {
        if (this.skip) {
            this.getLog().info("Skip execution.");
            return;
        }
        Wagon srcWagon = null;
        Wagon targetWagon = null;
        try {
            srcWagon = createWagon(sourceId, source);
            targetWagon = createWagon(targetId, target);
            copy(srcWagon, targetWagon);
        } catch (Exception e) {
            throw new MojoExecutionException("Error during performing repository copy", e);
        } finally {
            disconnectWagon(srcWagon);
            disconnectWagon(targetWagon);
        }
    }

    private void disconnectWagon(Wagon wagon) {
        try {
            if (wagon != null) {
                wagon.disconnect();
            }
        } catch (ConnectionException e) {
            getLog().debug("Error disconnecting wagon - ignored", e);
        }
    }

}